package com.mryan.metrics.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mryan.metrics.service.UserService;
import com.mryan.metrics.utils.Info;
import com.mryan.metrics.utils.Metrics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.websocket.server.PathParam;
import java.util.HashMap;
import java.util.Map;

/**
 * @description：TODO
 * @Author MRyan
 * @Date 2021/1/24 11:35
 * @Version 1.0
 */

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/register")
    @Metrics(realtimeDisplay = true,showAllMessageByTimeUnit = true)
    @Info(display = true)
    public Object registerUser(@PathParam("username") String username, @PathParam("pwd") String pwd) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, String> map = new HashMap<>();
        boolean b = userService.registerUser(username, pwd);
        map.put("code", b + "");
        System.out.println("方法返回值");
        return objectMapper.writeValueAsString(map);
    }


    @RequestMapping("/login")
    @Metrics(realtimeDisplay = false, showAllMessageByTimeUnit = true)
    public Object login(@PathParam("username") String username, @PathParam("pwd") String pwd) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, String> map = new HashMap<>();
        boolean b = userService.login(username, pwd);
        map.put("code", b + "");
        return objectMapper.writeValueAsString(map);
    }
}
